<?php

	require_once("../libs/constants.php"); 
	require_once("../libs/classes/mysqlClass.php"); 
	require_once("../libs/functions/functions.php"); 
	require_once("../libs/functions/site_functions.php");
	require_once("../libs/functions/category_functions.php"); 
	require_once("../libs/functions/site_page_functions.php");
	require_once("../libs/functions/article_functions.php");
	require_once("../libs/functions/product_functions.php");
	require_once("../libs/functions/comment_functions.php");
	require_once("../libs/functions/account_functions.php");
	require_once("../libs/functions/cart_functions.php");
	
	$site = verify_site();
	if($site == false)
	{
		return false;
	}
		
	
	$products = filter_products(false, false, null, 0, "name", "asc", false, false);
	
	$xml = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
 
    $xml .= '<rss version="2.0">' . "\n";
 
    // channel required properties
    $xml .= '<channel>' . "\n";
    $xml .= '<title> RSS Feed - ' . $site["name"] . '</title>' . "\n";
    $xml .= '<link>' . scripts_url() . '</link>' . "\n";
    $xml .= '<description>' . $site["description"] . '</description>' . "\n";
 
    // channel optional properties
      $xml .= '<language>' . $_SESSION["language"]["name"] . '</language>' . "\n";
      
      $xml .= '<image>' . "\n";
      $xml .= '<title>' . $site["name"] . '</title>' . "\n";
      $xml .= '<link>' .  scripts_url() . '</link>' . "\n";
      $xml .= '<url>' . scripts_url().$site["logo"] . '</url>' . "\n";
      $xml .= '</image>' . "\n";
 
    // get RSS channel items
    $now =  date("YmdHis"); // get current time  // configure appropriately to your environment
    $rss_items = $products;
 
    foreach($rss_items as $rss_item) {
      $xml .= '<item>' . "\n";
      $xml .= '<title>' . $rss_item['name'] . '</title>' . "\n";
      $xml .= '<link>' . $rss_item['url'] . '</link>' . "\n";
      $xml .= '<description>' . htmlentities($rss_item['description']) . '</description>' . "\n";
      
 //TODO: COMPLETE THIS     
      //$xml .= '<pubDate>' . $rss_item['pubDate'] . '</pubDate>' . "\n";
      	if(isset($rss_item["category"]["name"]))
	  	{
      		$xml .= '<category>' . $rss_item["category"]["name"] . '</category>' . "\n";
  		}
		elseif(isset($rss_item["parent_category"]["name"]))
		{
			$xml .= '<category>' . $rss_item["parent_category"]["name"] . '</category>' . "\n";
		}
		
		$xml .= '<comments>' . translate("price") . " : " . strip_tags($rss_item["price"]) . '</comments>' . "\n";
 
 
      $xml .= '</item>' . "\n";
    }
 
    $xml .= '</channel>';
 
    $xml .= '</rss>';
	
	$file = '../rss/'.$site["name"].'.xml';
	
	$fp = fopen($file,"wb");
	fwrite($fp,$xml);
	fclose($fp);
	
	destroyConnection();
	
?>